package org.example.springboot.controller;


import org.example.springboot.service.SuperUserService;
import org.example.springboot.common.Result;
import org.example.springboot.entity.SuperUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping
public class SuperUserController {

    @Autowired
    SuperUserService superUserService;

    @PostMapping("/login")
    public Result login(@RequestBody SuperUser superUser) {
        if (superUser.getAccount() == null || superUser.getPassword() == null) {
            return Result.error("账号或密码不能为空");
        } else {
            SuperUser selectUser = superUserService.selectUserById(superUser);
            if (selectUser == null) {
                return Result.error("用户不存在");
            } else if (!selectUser.getPassword().equals(superUser.getPassword())) {
                return Result.error("账号或密码错误");
            } else {

                return Result.success(selectUser);
            }
        }
    }

    @PostMapping("/register")
    public Result register(@RequestBody SuperUser superUser) {
        if(superUser.getAccount()==null ||superUser.getPassword()==null)
            return Result.error("账号或密码不能为空");
        else {
            SuperUser selectUser = superUserService.selectUserById(superUser);
            if(selectUser!=null)
                return Result.error("账号已经存在");
            else{
                superUserService.insertUser(superUser);
                return Result.success();
            }
        }

    }

    @PostMapping("/superUser/update")
    public Result superUserUpdate(@RequestBody SuperUser superUser) {
        superUserService.updateSuperUser(superUser);
        return Result.success();
    }


}
